package com.yc.springmvc.web;

import com.yc.springmvc.bean.Hall;
import com.yc.springmvc.bean.Movie;
import com.yc.springmvc.bean.Plan;
import com.yc.springmvc.bean.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @RequestMapping("gohome.do")
    String gohemo(){
        return "redirect:home.html";
    }

    //ResponseBody 指定返回对象转成json 格式
    @ResponseBody
    @RequestMapping("result.do")
    Result getResult(){
        return new Result(1,"now", new Date());
    }


    @RequestMapping("showName")
    @ResponseBody
    public String showName(
            @RequestParam("myname") String name,
            @RequestParam(value = "myage",required = true,defaultValue = "18")int age){
            return "my name is" + name + ", age is " + age;
    }

    @ResponseBody
    @RequestMapping("user/{myid}/{mysn}")
    public String myId(@PathVariable("myid") int id,
                       @PathVariable("mysn") String sn){
        return "my id is" + id + ", mysn is " + sn;
    }

    @ResponseBody
    @RequestMapping("headerParam")
    public String headerParam(
            @RequestHeader String Host,
            @RequestHeader(value = "Accept-Encoding") String acceptEncoding,
            @RequestHeader(value = "Content-Type" , defaultValue = "text") String contentType,
            @RequestHeader String Cookie,
            @CookieValue String myname
    ){
        return "Hosr = " + Host +
                "acceptEncoding" + acceptEncoding +"," +
                "Cookie" + Cookie +"," +
                "myname" + myname;
    }

    @RequestMapping("toth.do")
    public String toThymeleaf(Model model){
        model.addAttribute("msg","欢迎来到thmeleaf的世界");
        //WEB-INF/tempates/index.html
        return "index";
    }


    //127.0.0.1/xxx/halls.do?el=table => 表格中展示数据
    //127.0.0.1/xxx/halls.do?el=ul => 列表中展示数据
    @RequestMapping("halls.do")
    //如果没有方法的返回值定义,那么方法名即视图名
    public void showHalls(Map<String,Object> data){
        List<Hall> halls = new ArrayList<>();
        halls.add(new Hall(1,"豪华3D",true,"大", (short) 100));
        halls.add(new Hall(2,"豪华5D",true,"大", (short) 110));
        halls.add(new Hall(3,"豪华4D",true,"小", (short) 120));
        halls.add(new Hall(4,"豪华6D",true,"大", (short) 130));
        data.put("halls",halls);
    }

    @RequestMapping("plan.do")
    public void toplan(){

    }


    @ResponseBody
    @RequestMapping("addPlan")
    public Plan addPlan(Plan plan){
        return plan;
    }

    @ResponseBody
    @RequestMapping("addMovie")
    public Result addMovie(@Valid Movie movie, Errors errors){
        if (errors.hasFieldErrors()){
            errors.getFieldErrors().forEach(System.out::println);
            return new Result(0,"字段验证错误",errors.getFieldErrors());
        }
        return new Result(1,"添加成功",null);
    }

    @ResponseBody
    @RequestMapping("uploadImage")
    public Result uploadImage(@RequestParam("image")MultipartFile file) throws IOException {
        //文件名 =》 xxx.jpg
        String originalFilename = file.getOriginalFilename();
        //字段名 =》image
        file.getName();
        file.transferTo(new File("D:/源辰/s3/视频/0609/"+originalFilename));
        String webPath = "upload_image" + originalFilename;
        return new Result(1,"文件上传成功",webPath);
    }
}
